package com.ti.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.net.URL;

/**
 * @description: com.ti.demo.controller
 * @Author Triagen
 * @Date 2025/7/18 10:53
 */
@RequestMapping("/multimodality")
@RestController
public class ChatController {

    @Qualifier("ollamaChatClient")
    @Autowired
    private ChatClient ollamaChatClient;

    @Qualifier("deepSeekChatClient")
    @Autowired
    private ChatClient deepSeekChatClient;


    @GetMapping(value = "/describePicture")
    Flux<String> describePicture(@RequestParam(defaultValue = "Explain what do you see on this picture?") String text, String fileName) {
        return deepSeekChatClient.prompt()
                .user(u -> u.text(text)
                        .media(MimeTypeUtils.ALL, new ClassPathResource("images/" + fileName)))
                .stream().content();
    }

    @GetMapping(value = "/describePictureUrl")
    Flux<String> describePictureUrl(String text, String url) {
        return deepSeekChatClient.prompt()
                .user(u -> {
                    try {
                        u.text(text)
                                .media(MimeTypeUtils.ALL, new UrlResource(url));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                })
                .stream().content();
    }


    @GetMapping(value = "/describeUrl")
    Flux<String> describeUrl(String text, String url) {
        return deepSeekChatClient.prompt()
                .user(u -> {
                    try {
                        u.text(text)
                                .media(MimeTypeUtils.ALL, new URL(url));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                })
                .stream().content();
    }


}
